package com.salinity.kun.algorithm;

/**
 * 正弦波插值
 * 
 * @author Administrator
 *
 */
public class SinInterpolation {

	private double x1, x2, v1 = 0, v2 = 0, rslt = 0.0;

	public SinInterpolation() {
	};

	public SinInterpolation(double x1, double v1, double x2, double v2) {
		this.x1 = x1;
		this.x2 = x2;
		this.v1 = v1;
		this.v2 = v2;
	}

	public void setParams(double x1, double v1, double x2, double v2) {
		this.x1 = x1;
		this.x2 = x2;
		this.v1 = v1;
		this.v2 = v2;
	}

	public double getValue(double x) {
		if (x < x1 || x > x2) {
			throw new RuntimeException("X is out of range [v1,v2]");
		}
		double theta = (x - x1) / (x2 - x1) * Math.PI /2;
		rslt = v1 + (v2 - v1) * Math.sin(theta);
		return rslt;

	}

	public double getX1() {
		return x1;
	}

	public void setX1(double x1) {
		this.x1 = x1;
	}

	public double getX2() {
		return x2;
	}

	public void setX2(double x2) {
		this.x2 = x2;
	}

	public double getV1() {
		return v1;
	}

	public void setV1(double v1) {
		this.v1 = v1;
	}

	public double getV2() {
		return v2;
	}

	public void setV2(double v2) {
		this.v2 = v2;
	}

	public static void main(String[] args) {
		SinInterpolation SI = new SinInterpolation(3.02, 20.65, 11.45, 2.05);
		double dt = 0.5;
		double x = 3.02, y;

		while (x <= 11.45) {
			y = SI.getValue(x);
			System.out.println(x + "\t" + y);
			x += dt;
		}

	}
}
